package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.biz.CategoryBizService;
import com.atguigu.gmall.product.entity.*;
import com.atguigu.gmall.feign.search.SearchFeignClient;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.mapper.SkuInfoMapper;
import com.atguigu.gmall.product.service.SkuAttrValueService;
import com.atguigu.gmall.product.service.SkuImageService;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.product.service.SkuSaleAttrValueService;
import com.atguigu.gmall.product.vo.SkuInfoSaveVo;
import com.atguigu.gmall.search.entiy.Goods;
import com.atguigu.gmall.search.entiy.SearchAttr;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;


@Service
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfo>
    implements SkuInfoService{

    @Autowired
    private SkuInfoMapper skuInfoMapper;

    @Autowired
    private SkuImageService skuImageService;

    @Autowired
    private SkuAttrValueService skuAttrValueService;

    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;

    /**
     * sku大保存
     * @param skuInfoSaveVo
     */
    @Transactional
    @Override
    public void saveSkuInfo(SkuInfoSaveVo skuInfoSaveVo) {
        //1.sku_info保存
        SkuInfo skuInfo = new SkuInfo();
        BeanUtils.copyProperties(skuInfoSaveVo,skuInfo);
        this.save(skuInfo);
        //获取sku_id
        Long skuId = skuInfo.getId();
        //2.skuImageList保存
        List<SkuInfoSaveVo.SkuImageListDTO> skuImageList = skuInfoSaveVo.getSkuImageList();
        List<SkuImage> images = skuImageList.stream()
                .map(item -> {
                    SkuImage skuImage = new SkuImage();
                    BeanUtils.copyProperties(item, skuImage);
                    //回填
                    skuImage.setSkuId(skuId);
                    return skuImage;
                }).collect(Collectors.toList());
        skuImageService.saveBatch(images);
        //3.skuAttrValueList保存
        List<SkuInfoSaveVo.SkuAttrValueListDTO> skuAttrValueList = skuInfoSaveVo.getSkuAttrValueList();
        List<SkuAttrValue> skuAttrValues = skuAttrValueList.stream()
                .map(item -> {
                    SkuAttrValue skuAttrValue = new SkuAttrValue();
                    BeanUtils.copyProperties(item, skuAttrValue);
                    //回填
                    skuAttrValue.setSkuId(skuId);
                    return skuAttrValue;
                }).collect(Collectors.toList());
        skuAttrValueService.saveBatch(skuAttrValues);

        //4.skuSaleAttrValueList保存
        List<SkuInfoSaveVo.SkuSaleAttrValueListDTO> skuSaleAttrValueList = skuInfoSaveVo.getSkuSaleAttrValueList();
        List<SkuSaleAttrValue> skuSaleAttrValues = skuSaleAttrValueList.stream()
                .map(item -> {
                    SkuSaleAttrValue skuSaleAttrValue = new SkuSaleAttrValue();
                    BeanUtils.copyProperties(item, skuSaleAttrValue);
                    //回填
                    skuSaleAttrValue.setSkuId(skuId);
                    skuSaleAttrValue.setSpuId(skuInfoSaveVo.getSpuId());
                    return skuSaleAttrValue;
                }).collect(Collectors.toList());
        skuSaleAttrValueService.saveBatch(skuSaleAttrValues);
    }

    /**
     * 改变销售状态
     * @param skuId
     */
    private void changeOnSale(Long skuId,int status) {
        skuInfoMapper.updateSaleStatus(skuId,status);
        //todo 上架商品可以被检索到
    }

    /**
     * 改变销售状态
     * @param skuId
     */
    private void canceSale(Long skuId,int status) {
        skuInfoMapper.updateSaleStatus(skuId,status);
    }

    /**
     * 查询实时价格
     * @param skuId
     * @return
     */
    @Override
    public BigDecimal getPrice(Long skuId) {
        BigDecimal result = skuInfoMapper.getPrice(skuId);
        return result;
    }

    /**
     * 获取所有的skuid
     * @return
     */
    @Override
    public List<Long> getAllSkuId() {
        List<Long> list = skuInfoMapper.getAllSkuId();
        return list;
    }

    @Autowired
    SearchFeignClient searchFeignClient;
    /**
     * 上架
     * @param skuId
     */
    @Override
    public void onSale(Long skuId) {
        //该状态
        this.changeOnSale(skuId,1);
        //将sku保存到es中
        Goods goods = prepareGoods(skuId);
        searchFeignClient.onSaleGoods(goods);
    }

    /**
     * 下架
     * @param skuId
     */
    @Override
    public void canceSale(Long skuId) {
        this.changeOnSale(skuId,0);
        searchFeignClient.cancelSale(skuId);
    }

    @Autowired
    BaseTrademarkMapper baseTrademarkMapper;

    @Autowired
    CategoryBizService categoryBizService;

    @Autowired
    ThreadPoolExecutor executor;

//    @Autowired
//    SkuAttrValueService skuAttrValueService;


    /**
     * 生成商品存储的数据模型
     * @param skuId
     * @return
     */
    private Goods prepareGoods(Long skuId) {
        Goods goods = new Goods();
        SkuInfo skuInfo = skuInfoMapper.selectById(skuId);
        //sku基本信息
        CompletableFuture<Void> skuInfoFuture  = CompletableFuture.runAsync(() -> {
            goods.setId(skuId);
            goods.setDefaultImg(skuInfo.getSkuDefaultImg());
            goods.setTitle(skuInfo.getSkuName());
            goods.setPrice(skuInfo.getPrice());
            goods.setCreateTime(new Date());
        }, executor);


        //查询品牌
        CompletableFuture<Void> tradeMarkFuture  = CompletableFuture.runAsync(() -> {
            BaseTrademark trademark = baseTrademarkMapper.selectById(skuInfo.getTmId());
            goods.setTmId(trademark.getId());
            goods.setTmName(trademark.getTmName());
            goods.setTmLogoUrl(trademark.getLogoUrl());
        }, executor);


        //分类查询
        CompletableFuture<Void> categoryFuture  = CompletableFuture.runAsync(() -> {
            CategoryView view = categoryBizService.getCategory(skuId);
            goods.setCategory1Id(view.getC1id());
            goods.setCategory1Name(view.getC1name());
            goods.setCategory2Id(view.getC2id());
            goods.setCategory2Name(view.getC2name());
            goods.setCategory3Id(view.getC3id());
            goods.setCategory3Name(view.getC3name());
        }, executor);


        //热度分
        goods.setHotScore(0L);

        //查询商品的平台属性名和值
        CompletableFuture<Void> attrFuture  = CompletableFuture.runAsync(() -> {
            List<SearchAttr> searchAttrs = skuAttrValueService.getSkuAttrAndValue(skuId);
            goods.setAttrs(searchAttrs);
        }, executor);

        CompletableFuture.allOf(skuInfoFuture,tradeMarkFuture,categoryFuture,attrFuture).join();


        return goods;
    }


}




